package org.shoukaiseki.test.spring.springbootdemo.exceptionhandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 全局异常处理器
 * <br>
 * org.shoukaiseki.test.spring.springbootdemo.exceptionhandler.GlobalExceptionHandler
 * <br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br> E-メール jiang28555@Gmail.com<br>
 * @date 2020-12-05 22:28:07<br>
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 权限校验失败 如果请求为ajax返回json，普通请求跳转页面
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, Exception e)
    {
        log.error(e.getMessage(), e);
        return Map.of("code","500","msg",e.getMessage());
    }
}
